// pages/service/drug-control/add-drug/index.ts
import { addDrug } from '../../../../utils/api/drugControl'
import { fixZeroStart } from '../../../../utils/util'
import userBaseInfoState from '../../../../stores/userBaseInfo'
import Notify from '@vant/weapp/notify/notify';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    drugItem: <EatDrugAlertEntity>{
      patientUid: 123,
      drugName: '',
      frequency: '',
      unit: '',
      dose: 0,
      isActive: 1,
    },
    showEatTime: false,
    showAlertTime: false,
    showUnit: false,
    timeList: [
      {
        className: 'hour',
        values: new Array(24).fill(1).map((v, i) => i),
      },
      {
        className: 'minute',
        values: new Array(60).fill(1).map((v, i) => i),
      }
    ],
    eatTimeList: <string[]>[],
    alertTimeList: ['每天', ...Array.from({ length: 15 }, (v, k) => `每隔${k + 1}天`)],
    unitColumns: [
      '毫克',
      '毫升',
      '微克',
      '克',
      '片',
      '支',
      '粒',
      '包',
      '瓶',
    ],
    currentTime: '',
    minHour: 0,
    maxHour: 24,
    formatter(type: string, value: any) {
      if (type === 'hour') {
        return `${value}时`;
      }
      if (type === 'minute') {
        return `${value}分`;
      }
      return value;
    },
  },
  showEatTimePop() {
    this.setData({
      showEatTime: true,
    })
  },
  closeEatTimePop() {
    this.setData({
      showEatTime: false,
    })
  },
  showAlertTimePop() {
    this.setData({
      showAlertTime: true,
    })
  },
  closeAlertTimePop() {
    this.setData({
      showAlertTime: false,
    })
  },
  showUnitPop() {
    this.setData({
      showUnit: true,
    })
  },
  closeUnitPop() {
    this.setData({
      showUnit: false,
    })
  },
  changeDrugName(event:any) { 
    this.data.drugItem.drugName = event.detail;
    this.setData({
      drugItem: this.data.drugItem
    })
  },
  onInputTime(event: any) {
    this.setData({
      currentTime: event.detail,
    });
  },
  changeEatTime(event: any) {
    let time = event.detail;
    console.log(time)
    // 添加到List中
    this.data.eatTimeList.push(time);
    this.setData({
      eatTimeList: this.data.eatTimeList,
      showEatTime: false,
    })
  },
  deleteTime(evnet: any) {
    let index = evnet.currentTarget.dataset.index;
    console.log(index);
    let list = this.data.eatTimeList;
    list.splice(index,1);
    this.setData({
      eatTimeList: list
    })
  },
  changeAlertTime(event: any) {
    const { picker, value, index } = event.detail;
    this.data.drugItem.frequency = value
    this.setData({
      drugItem: this.data.drugItem,
      showAlertTime: false,
    })
  },
  changeUnit(event: any) {
    const { picker, value, index } = event.detail;
    this.data.drugItem.unit = value;
    this.setData({
      drugItem: this.data.drugItem,
      showUnit: false,
    })
  },
  changeDose(event:any) { 
    this.data.drugItem.dose = event.detail;
    this.setData({
      drugItem: this.data.drugItem
    })
  },

  cancelSubmit() {
    wx.navigateBack()
  },
  confirmSubmit() {
    const dataToAdd = {
      patientUid: this.data.drugItem.patientUid,
      drugName: this.data.drugItem.drugName,
      frequency: this.data.drugItem.frequency,
      unit: this.data.drugItem.unit,
      dose: this.data.drugItem.dose,
      isActive: 1, // 默认启用该药品
      eatTime: this.data.eatTimeList,
    }
    console.log(dataToAdd);
    addDrug(dataToAdd).then((res: any) => {
      console.log('添加成功', res);
      wx.navigateBack();
    }).catch((err: any) => {
      console.log('添加失败', err);
    })
  },
  submit() {
    // 校验各种规则
    // 校验名称是否为空
    if (this.data.drugItem.drugName == null || this.data.drugItem.drugName == '') {
      Notify({ type: 'danger', message: '药品名称不能为空' })
      return;
    }
    // 查看用药时间
    if (this.data.eatTimeList == null || this.data.eatTimeList.length == 0) {
      Notify({ type: 'danger', message: '请添加用药时间' })
      return;
    }
    // 校验用药频次
    if (this.data.drugItem.frequency == null || this.data.drugItem.frequency == '') {
      Notify({ type: 'danger', message: '请添加用药频次' })
      return;
    }
    // 校验单位
    if (this.data.drugItem.unit == null || this.data.drugItem.unit == '') {
      Notify({ type: 'danger', message: '请添加单位' })
      return;
    }
    // 校验剂量
    if (this.data.drugItem.dose == null || this.data.drugItem.dose <= 0 ) {
      Notify({ type: 'danger', message: '请检查剂量输入是否正确' })
      return;
    }
    // TODO: 正式提交
    this.confirmSubmit();
  },
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.setData({
      'drugItem.patientUid': userBaseInfoState.data.patientUid
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})